package com.tsn.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsn.common.Result;
import com.tsn.entity.Comment;
import com.tsn.entity.Forum;
import com.tsn.mapper.ForumMapper;
import com.tsn.service.CommentService;
import com.tsn.service.ForumService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 论坛 前端控制器
 * </p>
 *
 * @author tsn
 * @since 2021-09-07
 */
@RestController
@RequestMapping("/tsn/forum")
public class ForumController {
    @Autowired
    private ForumService forumService;

    @GetMapping("/getForums")
    public Result getForums(){
        QueryWrapper<Forum> forumQueryWrapper = new QueryWrapper<>();
        forumQueryWrapper.orderByDesc("update_time");
        return Result.success(forumService.list(forumQueryWrapper));
    }
    @RequiresAuthentication
    @PostMapping("/addForum")
    public Result addForum(@RequestBody JSONObject object){
        System.out.println(object);
        Forum forum = new Forum();
        forum.setForumTitle(object.getString("title"));
        forum.setLandlord(object.getString("userName"));
        forum.setUserId(object.getInteger("userId"));
        Comment comment = new Comment();
        comment.setContent(object.getString("content"));
        comment.setUserId(object.getInteger("userId"));
//        comment.set
//        String content = object.getString("content");
        return forumService.addForums(forum,comment);
    }
    @GetMapping("/selectForumByWord")
    public Result selectForumByWord(@RequestParam("word") String word){
        return forumService.searchForumByWords(word);

    }
    @GetMapping("/selectMyForums")
    public Result selectMyForums(@RequestParam("userId")Integer id){
        QueryWrapper<Forum> forumQueryWrapper = new QueryWrapper<>();
        forumQueryWrapper.eq("user_id",id);
        forumQueryWrapper.orderByDesc("update_time");
//        forumService.list(forumQueryWrapper);
        return Result.success(forumService.list(forumQueryWrapper));

    }
    @GetMapping("/getForumById")
    public Result getForumById(@RequestParam("id")Integer id){
        return Result.success(forumService.getById(id));

    }
}

